package com.ktjiaoyu.service.impl;

import com.ktjiaoyu.entity.StorageRecord;
import com.ktjiaoyu.entity.Supplier;
import com.ktjiaoyu.mapper.SupplierMapper;
import com.ktjiaoyu.service.SupplierService;
import com.ktjiaoyu.utils.Page;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SupplierServiceImpl implements SupplierService {
    private static final Logger LOGGER = LoggerFactory.getLogger(SupplierService.class);
    @Autowired
    SupplierMapper supplierMapper;

    @Override
    public Page<Supplier> findPage(Supplier supplier, Integer pageNo, Integer pageSize) throws Exception {
        Page<Supplier> page = null;
        try {
            page = new Page<>();
            Long count = supplierMapper.count(supplier);
            page.setPageNo(pageNo);
            page.setPageSize(pageSize);
            page.setTotalCount(count.intValue());
            List<Supplier> data = supplierMapper.getPage(supplier, page.offset(), page.getPageSize());
            page.setData(data);
        } catch (Exception e) {
            LOGGER.error("执行SupplierServiceImpl.findPage查询分页信息异常", e);
            throw new RuntimeException(e.getMessage());
        }
        return page;
    }

    @Override
    public boolean saveOrUpdate(Supplier supplier) throws Exception {
        try {
            if (supplier.getId() != null) {
                return supplierMapper.update(supplier) > 0;
            } else {
                return supplierMapper.insert(supplier) > 0;
            }
        } catch (Exception e) {
            LOGGER.error("SupplierServiceImpl.saveOrUpdate(Supplier)方法出现异常", e);
            throw e;
        }
    }

    @Override
    public Supplier findById(Long id) throws Exception {
        try {
            return supplierMapper.findById(id);
        } catch (Exception e) {
            LOGGER.error("SupplierServiceImpl.findById(Long)方法出现异常", e);
            throw e;
        }
    }

    @Override
    public List<Supplier> findAll() throws Exception {
        return supplierMapper.getAll();
    }

    @Override
    public boolean deleteById(Long id) {
        return supplierMapper.deleteById(id) > 0;
    }
}
